package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.SetmealService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.models.auth.In;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 获取今日数据
     *
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {

        log.info("开始时间：{}，结束时间：{}", beginTime, endTime);
        //获取新增用户数量
        Integer newUsers = userMapper.getUserCount(beginTime, endTime);

        //获取营业额
        Map map = new HashMap();
        map.put("status", Orders.COMPLETED);
        map.put("begin", beginTime);
        map.put("end", endTime);
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;

        //获取订单完成率
        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        Integer orderCount = orderMapper.getOrderCount(beginTime, endTime, null);
        Integer validOrderCount = orderMapper.getOrderCount(beginTime, endTime, Orders.COMPLETED);
        if (orderCount != 0 && validOrderCount != 0){
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
            //获取平均客单价
            unitPrice = turnover.doubleValue() / validOrderCount;
        }
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 订单总览
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        LocalDateTime beginTime = LocalDateTime.now().with(LocalTime.MIN);
        Map map = new HashMap();
        map.put("begin",beginTime);
        map.put("status",null);
        //获取全部订单数量
        Integer allOrders = orderMapper.countByMap(map);

        //获取已取消的订单数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //获取已完成的订单数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //获取待派送的订单数量
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //获取待接单的数量
        map.put("status",Orders.PENDING_PAYMENT);
        Integer waitingOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        //统计停售的菜品
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);

        //统计已起售的菜品
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        //统计停售的套餐
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);

        //统计已起售的套餐
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
